<template>
  <div class="fans-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>粉丝管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-tabs type="border-card" @tab-click="onFansTabClick">
        <el-tab-pane label="粉丝列表">
          <div style="margin-bottom: 20px;">
            <el-row :gutter="20">
              <el-col
                :xs="12"
                :sm="8"
                :md="6"
                :lg="4"
                :xl="2"
                v-for="(fan, index) in fans"
                :key="index"
              >
                <div class="fans-list">
                  <el-avatar class="fans-img" :src="fan.photo"></el-avatar>
                  <p>{{ fan.name }}</p>
                  <el-tag
                    :type="fan.followed ? 'danger':'primary'"
                    @click="onFollow(fan.id)"
                  >{{ fan.followed ? '取消关注' : '+关注' }}
                  </el-tag>
                </div>
              </el-col>
            </el-row>
          </div>
          <el-pagination
            background
            layout="prev, pager, next"
            :total="totalCount"
            :disabled="loading"
            :current-page.sync="page"
            @current-change="onFansCurrentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="粉丝画像">
          <!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
          <div ref="main" style="width: 600px;height:400px;"></div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { follow, queryFollowers } from '@/api/fans'
import * as echarts from 'echarts'

export default {
  name: 'FansIndex',
  components: {},
  props: {},
  data () {
    return {
      page: 1,
      perPage: 10,
      totalCount: 0,
      fans: [],
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadFollowers()
  },
  mounted () {
    // 注意：初始化 dom 操作一定要写到 monted 钩子函数中
    // 基于准备好的dom，初始化echarts实例
    const myChart = echarts.init(this.$refs.main)

    // 指定图表的配置项和数据
    const option = {
      title: {
        text: 'ECharts 入门示例'
      },
      tooltip: {},
      legend: {
        data: ['销量']
      },
      xAxis: {
        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
      },
      yAxis: {},
      series: [{
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
      }]
    }

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option)
  },
  methods: {
    onFansTabClick () {
      console.log('onFansTabClick')
    },

    // 粉丝列表
    loadFollowers (page = 1) {
      this.page = page
      this.loading = true
      queryFollowers({
        page,
        per_page: this.perPage
      }).then(res => {
        const {
          results,
          total_count: totalCount
        } = res.data.data
        this.fans = results
        this.totalCount = totalCount
        this.loading = false
      })
    },

    // 切换当前页码
    onFansCurrentChange (page) {
      this.loadFollowers(page)
    },

    // 关注
    onFollow (fansId) {
      follow(fansId).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.loadFollowers(this.page)
      })
    }
  }
}
</script>

<style scoped lang="less">
.fans-list {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  width: 120px;
  height: 180px;
  border: 1px dotted #ccc;
  margin-bottom: 10px;
  padding-top: 10px;
  padding-bottom: 10px;

  .fans-img {
    width: 80px;
    height: 80px;
  }
}
</style>
